package com.zygh.lz.controller.pc;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zygh.lz.constant.SystemCon;
import com.zygh.lz.entity.Account;
import com.zygh.lz.entity.AccountConfig;
import com.zygh.lz.service.AccountConfigService;
import com.zygh.lz.service.AccountService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.sql.Wrapper;
import java.util.List;

/**
 * @program: zygh-transform
 * @description: 账户配置管理
 * @author: jhaol
 * @created: 2023/04/15 09:22
 */
@Api(tags = "账户配置")
@RestController
@RequestMapping("/accountConfig")
public class AccountConfigController {
    @Resource
    AccountConfigService accountConfigService;

    @Resource
    AccountService accountService;

    @GetMapping()
    @ApiOperation("获取账户配置信息")
    public ResultBean<AccountConfig> getAccountConfig(Principal principal) {
        Account account = accountService.getByUsername(principal.getName());
        AccountConfig accountConfig = accountConfigService.getOne(Wrappers.<AccountConfig>lambdaQuery().
                eq(AccountConfig::getSysStaffId, account.getSysStaffId())
                .last("limit 1"));
        if (accountConfig == null) {
            accountConfig = new AccountConfig();
            accountConfig.setSystemTitle("交警勤务动态管理平台");
        }
        return ResultUtil.setOK("success", accountConfig);
    }

    @PostMapping("/update")
    @ApiOperation("修改或添加账户配置信息")
    public ResultBean<AccountConfig> getAccountConfig(Principal principal,@RequestBody AccountConfig accountConfig) {

        Account account = accountService.getByUsername(principal.getName());
        accountConfig.setSysStaffId(account.getSysStaffId());
        boolean update = accountConfigService.saveOrUpdate(accountConfig, Wrappers.<AccountConfig>lambdaUpdate()
                .eq(AccountConfig::getSysStaffId, accountConfig.getSysStaffId())
                .set(AccountConfig::getSystemTitle, accountConfig.getSystemTitle()));
        return update ? ResultUtil.setOK("success", accountConfig) : ResultUtil.setError(500, "修改失败", accountConfig);
    }
}
